// pages/unpacking-securities/securities.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    rid: '',
    userid: '',

    couponlist: '',

    shi: '',
    fen: '',
    miao: '',
    islogin: false,

    PersonID: "",

    Province: '',
    City: '',
    District: '',

    status: 1,
    isself: '',

    urluserid: '',
    urlProvince: '',
    urlCity: '',
    urlDistrict:'',
    INZL:"",

    succeed:"",


    //头像
    avatar:"",
  },

  getlist: function(fenszid) {
    ajax('/api/ZLMDShare/getCBLQ?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token') ? wx.getStorageSync('Access_token') : this.data.token,
      ID: this.data.id,
      ReceiveCouponID: this.data.rid,
      PersonID: fenszid,
      Province: this.data.province,
      City: this.data.city,
      District: this.data.district
    }), {
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      // console.log(res)
      if (res.Models) {
        this.setData({
          couponlist: res.Models
        })
        for (var a in this.data.couponlist) {
          var sfm = this.data.couponlist[a].DJS
          var shuzu = sfm.split(":")
          console.log(shuzu, 'shuzu')
          this.setData({
            shi: shuzu[0],
            fen: shuzu[1],
            miao: shuzu[2]
          })
        }
      }

      if (fenszid != this.data.PersonID){
        this.setData({
          INZL: res.Models[0].INZL
        })
        if (res.Models[0].INZL==1){
          this.setData({
            succeed: 2
          })
        }
      }
    })

  },

  //  授权
  bindGetUserInfo(e) {
    console.log(e)

    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        wx.setStorageSync("code", res.code)
      }
    })

    var that = this;
    if (e.detail.userInfo) {
      // 用户按了允许授权按钮
      var code = wx.getStorageSync("code");

      this.setData({
        avatar: e.detail.userInfo.avatarUrl
      })

      ajax('/api/WXUserLogin/Insert?requestContent=' + JSON.stringify({
        code: code,
        nickname: e.detail.userInfo.nickName,
        avatar: e.detail.userInfo.avatarUrl,
        UserType: 3
      }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "post",
      }, res => {
        this.setData({
          islogin: true,
        })
        wx.setStorageSync("Access_token", res.Access_token)
        var PersonID = res.SignID

        if (this.data.urluserid != '') {
          if (PersonID == this.data.urluserid) {
            this.setData({
              status: 3,
              Province: this.data.urlProvince,
              City: this.data.urlCity,
              District: this.data.urlDistrict,
              PersonID: PersonID,
              isself:true
            })

            this.getlist(PersonID)

          } else {
            this.setData({
              status: 4,
              Province: this.data.urlProvince,
              City: this.data.urlCity,
              District: this.data.urlDistrict,
              PersonID: PersonID,
              isself: false,
              
            })
            this.getlist(this.data.urluserid)

          }
        }

      })
    } else {
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }
  },

  toindex:function(){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },


  bangcha: function() {
    ajax('/api/ZLMDShare/AddCBLQ?requestContent=' + JSON.stringify({
      token: wx.getStorageSync("Access_token"),
      CurunionID: this.data.id,
      ReceiveCouponID: this.data.rid,

      HeadImg: this.data.avatar,
      PersonID: this.data.PersonID

    }), {
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "POST",
    }, res => {
      console.log(res)
      if (res.Status==1){
        this.setData({
          succeed: 1
        })
      }
     
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    // 已登录 第一次进  status 1  去本地存储拿省市区  personid==本地存储  isself==true
    // 自己已登录点击进 status 2  去本地存储拿省市区  personid==本地存储  isself==true
    // 自己未登录点击进 status 3  去路由拿           personid==登录返回  isself==true
 
    // 好友未登录点击进 status 4  去路由拿           personid==登录返回  isself==false 

    // xx// 好友已登录点击进 status 5  去本地存储拿省市区  personid==本地存储  isself==false

    console.log(options)
    var id = options.id
    var rid = options.rid
    this.setData({
      // 优惠券 id  领取记录id
      id: id,
      rid: rid,
    })
    var userid = options.userid
    var Province = options.Province
    var City = options.City
    var District = options.District

    this.setData({
      urluserid: userid == undefined ? '' : userid,
      urlProvince: Province == undefined ? '' : Province,
      urlCity: City == undefined ? '' : City,
      urlDistrict: District == undefined ? '' : District
    })

    var token = wx.getStorageSync('Access_token')

    if (token != '') {
      this.setData({
        islogin: true
      })
      var PersonID = wx.getStorageSync('PersonID')
      if (userid != undefined||'') {
        if (PersonID == Number(userid)) {
          this.setData({
            status: 2,
            Province: wx.getStorageSync('address_component').province,
            City: wx.getStorageSync('address_component').city,
            District: wx.getStorageSync('address_component').district,
            PersonID: PersonID,
            isself:true,
          })
          
          this.getlist(PersonID)
        } else {

          this.setData({
            islogin:false
          })

        }
      } else {
        this.setData({
          status: 1,
          Province: wx.getStorageSync('address_component').province,
          City: wx.getStorageSync('address_component').city,
          District: wx.getStorageSync('address_component').district,
          PersonID: PersonID,
          isself: true,
        })
        this.getlist(PersonID)
      }
    } else {
      this.setData({
        islogin: false
      })
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    // var id = "id123456"

    // 正常进来转发

    var id = this.data.id
    var rid = this.data.rid

    var userid = this.data.PersonID

    var Province = this.data.Province
    var City = this.data.City
    var District = this.data.District


    console.log('/pages/unpacking-securities/securities?id=' + id + '&rid=' + rid + '&userid=' + userid + '&Province=' + Province + '&City=' + City + '&District=' + District,)

    var img = this.data.couponlist[0].ImgPath //  分享图片
    return {
      title: '897便利购', //转发的标题
      path: '/pages/unpacking-securities/securities?id=' + id + '&rid=' + rid + '&userid=' + userid + '&Province=' + Province + '&City=' + City + '&District=' + District, //转发的连接 后面带 id
      imageUrl: img,
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    } //转发的图片
  }
})